package com.ruoyi.user.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.ruoyi.user.common.Result;
import com.ruoyi.user.entity.Merchant;
import com.ruoyi.user.mapper.MerchantMapper;
import com.ruoyi.user.service.MerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MerchantServiceImpl implements MerchantService {
    @Autowired
    private MerchantMapper merchantMapper;

    @Override
    public Result merchantLogin(Merchant merchant) {
        if (StringUtils.isAnyBlank(merchant.getPhone(), merchant.getPassword())) {
            return Result.error("200", "手机号或密码不能为空");
        }
        List<Merchant> list = merchantMapper.merchantLogin(merchant);

        if (list.size() > 0) {
            return Result.success(list.get(0));
        }
        return Result.error("200", "用户名或密码错误");
    }

    @Override
    public Result merchantRegister(Merchant merchant) {
        String phone = merchant.getPhone();
        String password = merchant.getPassword();
        String contactPerson = merchant.getContactPerson();
        String idcard = merchant.getIdcard();
        String merchantName = merchant.getMerchantName();
        if (StringUtils.isAnyBlank(phone, password, contactPerson, idcard, merchantName)) {
            return Result.error("6", "填写内容不能为空");
        }
        List<Merchant> list = merchantMapper.selectMerchantByPhoneOrIdcard(merchant);
        if (list.size() > 0) {
            if (list.get(0).getPhone().equals(phone) && list.get(0).getIdcard().equals(idcard)) {
                return Result.error("6", "手机号和身份证已注册");
            }
            if (list.get(0).getPhone().equals(phone)) {
                return Result.error("6", "手机号已注册");
            }
            if (list.get(0).getIdcard().equals(idcard)) {
                return Result.error("6", "身份证号已注册");
            }
        }
        merchantMapper.merchantRegister(merchant);
        return Result.success("200", "注册成功");
    }

    @Override
    public Result update(Merchant merchant) {
        int result = merchantMapper.update(merchant);
        if (result > 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("200", "修改失败");
        }
//        String string = merchant.toString();
//        System.out.println(string);
//        return Result.success(string);

    }
}
